﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CPPEI.Coolzon.ViewState
{
    /// <summary>
    /// 组件的视图状态
    /// </summary>
    public class PartState
    {
        public string Name { get; set; }

        /// <summary>
        /// 状态信息
        /// </summary>
        public Dictionary<string, string> States { get; set; }

        [Obsolete("delete")]
        public List<PartState> SubPartStateList { get; set; }


        public PartState(string partName)
        {
            this.Name = partName;

            States = new Dictionary<string, string>();
            SubPartStateList = new List<PartState>();
        }

        public void SetString(string key, string value)
        {
            if (States.ContainsKey(key))
            {
                States[key] = value;
            }
            else
            {
                States.Add(key, value);
            }
        }

        public void SetInt(string key, int value)
        {
            States.Add(key, value.ToString());
        }

        public string GetString(string key)
        {
            if (States.ContainsKey(key))
            {
                return States[key];
            }
            return null;
        }

        public int GetInt(string key)
        {
            if (States.ContainsKey(key))
            {
                int value = 0;
                if (int.TryParse(States[key], out value))
                {
                    return value;
                }
            }
            return 0;
        }

        /// <summary>
        /// 设置字符串并转码，放置出现特殊字符
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetStringAndEncode(string key, string value)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(value);
            string encodeString = Convert.ToBase64String(bytes);
            SetString(key, encodeString);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string GetStringAndDecode(string key)
        {
            string originalString = GetString(key);
            if (!string.IsNullOrEmpty(originalString))
            {
                byte[] bytes = Convert.FromBase64String(originalString);
                return Encoding.UTF8.GetString(bytes);
            }
            return originalString;
        }

        /// <summary>
        /// 添加子组件的视图状态
        /// </summary>
        /// <param name="subPartState"></param>
        public void AddSubPartState(PartState subPartState)
        {
            SubPartStateList.Add(subPartState);
        }

        /// <summary>
        /// 获取组件的状态
        /// </summary>
        /// <param name="partName"></param>
        /// <returns></returns>
        public PartState GetSubPartState(string partName)
        {
            for (int index = 0; index < SubPartStateList.Count; index++)
            {
                if (SubPartStateList[index].Name == partName)
                {
                    return SubPartStateList[index];
                }
            }
            return null;
        }
    }
}
